/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * 
 */
package com.niannian.activity;

import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.niannian.dialog.TipDialog;
import com.niannian.dialog.TipDialog.onSimpleOnCancelListener;
import com.niannian.dialog.TipDialog.onSimpleOnClicklListener;
import com.niannian.util.AsyncHandle;
import com.niannian.util.Common;
import com.niannian.util.ErrorCode;
import com.niannian.util.MyTextWatcher;
import com.niannian.web.nnSyncApi;
import com.niannian.wxapi.WXEntryActivity;
import com.niannian.BaseActivity;
import com.niannian.R;

/**
 * 注册的第一步
 * 
 * @author shengl
 * 
 */
public class RegisterOneActivity extends BaseActivity {
	private EditText reg_phone_register, reg_password_register;
	private Button btn_register_one;
	String acct = "", passwd = "";
	private TipDialog tip;
	TextView tv_login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register_one);
		initView();
		// initDialog();
		setLister();
		initTopView();
		addActToGroup("Register", this);
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("注册");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myfinish();
				// tip.show();
			}
		});
	}

	private MyTextWatcher textWatcher = new MyTextWatcher() {
		@Override
		public void onFocusChange(View view, boolean hasFocus) {
		};

		@Override
		public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
			String acct = reg_phone_register.getText().toString().trim();
			String upwd = reg_password_register.getText().toString().trim();
			if (!Common.empty(acct) && !Common.empty(upwd)) {
				// btn_register_one.setEnabled(true);
			} else {
				// btn_register_one.setEnabled(false);
			}
		}
	};

	private void initDialog() {
		tip = new TipDialog(RegisterOneActivity.this, "确认要放弃注册吗?");
		tip.setOnCancelListener(new onSimpleOnCancelListener() {

			@Override
			public void onCancel() {
				tip.dismiss();
			}
		});
		tip.setOnConfimClickListener(new onSimpleOnClicklListener() {

			@Override
			public void onClick() {
				myfinish();
				tip.dismiss();
			}
		});
	}

	private void initView() {
		reg_phone_register = (EditText) findViewById(R.id.reg_phone_register);
		reg_password_register = (EditText) findViewById(R.id.reg_password_register);
		btn_register_one = (Button) findViewById(R.id.btn_register_one);

		reg_phone_register.setOnFocusChangeListener(textWatcher);
		reg_phone_register.addTextChangedListener(textWatcher);
		reg_password_register.setOnFocusChangeListener(textWatcher);
		reg_password_register.addTextChangedListener(textWatcher);

		tv_login = (TextView) findViewById(R.id.tv_login);
		tv_login.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);// 下划线
	}

	private void setLister() {

		btn_register_one.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				acct = reg_phone_register.getText().toString();
				passwd = reg_password_register.getText().toString();

				if (acct.length() != 11) {
					Common.tip(RegisterOneActivity.this, "请输入您的11位手机帐号");
					return;
				}

				switch (Common.checkPassword(passwd)) {
				case 1:
					Common.tip(RegisterOneActivity.this, "密码应该是数字与字符的组合");
					return;
				case 2:
					Common.tip(RegisterOneActivity.this, "密码太短，请至少输入6个字符");
					return;
				case 3:
					Common.tip(RegisterOneActivity.this, "密码太长,请至多输入16个字符");
					return;
				}
				new GetCaptcha().init(RegisterOneActivity.this, null, true, "请稍等...").execute();
			}
		});

		tv_login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openActivity(WXEntryActivity.class);
				startAnimationLeftToRight();
				finish();
				destroyGroup("Register");
			}
		});

	}

	protected class GetCaptcha extends AsyncHandle {

		protected void errorFinally(Map<String, Object> paramMap) {
			Common.tip(RegisterOneActivity.this, "获取验证码失败");
		}

		protected void handleData(JSONObject paramJSONObject, Map<String, Object> paramMap) throws JSONException {
			int retCode = paramJSONObject.getInt("code");
			if (retCode == ErrorCode.OK) {
				Common.tip(RegisterOneActivity.this, "获取验证码成功");

				Bundle b = new Bundle();
				b.putString("phone", acct);
				b.putString("passwd", passwd);
				openActivity(RegisterTwoActivity.class, b);
				startAnimationLeftToRight();

			} else {
				Common.tip(RegisterOneActivity.this, paramJSONObject.getString("msg"));
			}
		}

		protected String runTask(Map<String, Object> paramMap) {
			return nnSyncApi.verifyPhone(acct, "signup");
		}
	}

	@Override
	public void onBackPressed() {
		// tip.show();
		myfinish();
	}
}
